<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8" />
        <meta http-equiv="X-UA-Compatible" content="ie=edge" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>Document</title>
        <style>
            .com {
                width: 150px;
                height: 50px;
                border: 1px solid;
            }
        </style>
    </head>
    <body>
        <div id="app">
            <button v-for="item in btnList" :key="item.title" @click="handleClick(item.title)">
                {{item.name}}
            </button>
            <keep-alive :max="2">
                <component :is="comp"></component>
            </keep-alive>
        </div>
        <template id="news">
            <div>
                <input type="text" v-model="val1" />
            </div>
        </template>
        <template id="happy">
            <div>
                <!-- <input type="text" v-model="val2" /> -->
                <div class="com">娱乐</div>
            </div>
        </template>
        <template id="sports">
            <div>
                <input type="text" v-model="val3" />
            </div>
        </template>
        <script src="../vue.js"></script>
        <script>
            let news = {
                template: '#news',
                data() {
                    return {
                        val1: '',
                    };
                },
                activated() {
                    console.log('news - activated');
                },
                deactivated() {
                    console.log('news - deactivated');
                },
            };
            let happy = {
                template: '#happy',
                data() {
                    return {
                        val2: '',
                    };
                },
            };
            let sports = {
                template: '#sports',
                data() {
                    return {
                        val3: '',
                    };
                },
                activated() {
                    console.log('sports - activated');
                },
                deactivated() {
                    console.log('sports - deactivated');
                },
            };
            let vm = new Vue({
                el: '#app',
                data() {
                    return {
                        comp: 'news',
                        btnList: [
                            { title: 'news', name: '新闻' },
                            { title: 'happy', name: '娱乐' },
                            { title: 'sports', name: '体育' },
                        ],
                    };
                },
                methods: {
                    handleClick(type) {
                        if (type === 'news') {
                            this.comp = type;
                        } else if (type === 'happy') {
                            this.comp = type;
                        } else if (type === 'sports') {
                            this.comp = type;
                        }
                    },
                },
                components: {
                    news,
                    happy,
                    sports,
                },
            });
        </script>
    </body>
</html>
